<?php

namespace Modules\Api\Http\Controllers;

use HiCommon\Service\LogService;
use HiCommon\Service\OrderCheckpointService;
use HiCommon\Service\OrderPayService;
use HiCommon\Service\PayYanSongDaService;
use HiCommon\Service\ShopCheckpointAppriseService;
use Illuminate\Support\Facades\Request;
use Modules\Api\Service\CheckpointOrderService;

class OrderCheckpointController extends BaseController
{

    /**
     * 年检订单提交预约 - 个人单 - 到站检车
     *
     * @throws \Exception
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function personal_goto_submit()
    {
        try {
//            $arr_input = Request::input();
            $n_uid = intval(api_user_id());
            $arr_user = [
              'phone'=>api_user()->phone,
              'realname'=>api_user()->realname,
            ];
            shiwu_start();
            $s_request_data =  Request::input('request_data');
            $arr_input = json_decode($s_request_data,true);
            $res = OrderCheckpointService::api_submit_personal_autonomy($n_uid,$arr_input,$arr_user);
            shiwu_end($res);
            return $res;
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }

    }


    /**
     * 年检订单提交预约 - 个人单 - 代驾检车
     *
     * @throws \Exception
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function personal_helpdrivingto_submit()
    {
        try {
//            $arr_input = Request::input();
            $n_uid = (int)api_user_id();
            $arr_user = [
              'phone'=>api_user()->phone,
              'realname'=>api_user()->realname,
            ];
            shiwu_start();
            $s_request_data =  Request::input('request_data');
            $arr_input = json_decode($s_request_data,true);
            $res = OrderCheckpointService::api_submit_personal_helpdriving($n_uid,$arr_input,$arr_user);
            shiwu_end($res);
            return $res;
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }

    }



    /**
     * 年检订单提交预约 - 企业单 - 到站检车
     *
     * @throws \Exception
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function company_goto_submit()
    {
        try {
//            $arr_input = Request::input();
            $n_uid = intval(api_user_id());
            $arr_user = [
              'phone'=>api_user()->phone,
              'realname'=>api_user()->realname,
            ];
            shiwu_start();
            $s_request_data =  Request::input('request_data');
            $arr_input = json_decode($s_request_data,true);
            $res = OrderCheckpointService::api_submit_company_autonomy($n_uid,$arr_input,$arr_user);
            shiwu_end($res);
            return $res;
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }

    }

    /**
     * 检测机构-年检预约单-到站检车转为代驾
     *
     * @throws \Exception
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_to_helpdriving()
    {
        try {

            shiwu_start();
//            $arr_input =  Request::input();
            $s_request_data =  Request::input('request_data');
            $arr_input = json_decode($s_request_data,true);
            $res = OrderCheckpointService::api_personal_change_to_helpdriving($arr_input);
            shiwu_end($res);
            return $res;
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }

    }

    /**
     * 确认完成订单
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function confirm_complete(){
        try {
            $n_id = intval(Request::input('id',0));
            $n_uid = intval(api_user_id());
            shiwu_start();
            $res = OrderCheckpointService::api_order_confirm_complete($n_uid,$n_id);
            shiwu_end($res);
            return $res;
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }


    /**
     * 检测机构-预约订单评价
     *
     * @return array|string
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function apprise_submit()
    {
        try {
            $arr_input = Request::input();
            $n_uid = intval(api_user_id());
            shiwu_start();
            $res = ShopCheckpointAppriseService::api_shop_arrpise_submit($n_uid,$arr_input);
            shiwu_end($res);
            return $res;
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }


    }

//    /**
//     * 年检预约单-申请退款-整单
//     *
//     * @return array
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function apply_refund()
//    {
//        try {
//
//            $n_uid = intval(api_user_id());//用户id
//            $arr_input = Request::input();
//
//            //开启事务
//            shiwu_start();
//            $result = OrderCheckpointService::api_apply_refund($n_uid,$arr_input);
//            shiwu_end($result);
//
//            return  $result;
//        }
//        catch (\Exception $exception) {
//            return hello_error('失败',[],$exception->getMessage());
//        }
//    }

//    /**
//     * 年检预约单-申请退款-单个车辆
//     *
//     * @return array
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function apply_refund_for_vehicle()
//    {
//        try {
//            $n_uid = intval(api_user_id());//用户id
//            $arr_input = Request::input();
//
//            //开启事务
//            shiwu_start();
//            $result = OrderCheckpointService::api_apply_refund_for_vehicle($n_uid,$arr_input);
//            shiwu_end($result);
//
//            return  $result;
//        }
//        catch (\Exception $exception) {
//            return hello_error('失败',[],$exception->getMessage());
//        }
//    }

    /**
     * 年检预约单-申请退款（批量）-多选车辆
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function apply_refund_for_more_vehicle()
    {
        try {
            $n_uid = intval(api_user_id());//用户id
            $arr_input = Request::input();

            //开启事务
            shiwu_start();
            $result = OrderCheckpointService::api_apply_refund_for_more_vehicle($n_uid,$arr_input);
            shiwu_end($result);

            return  $result;
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 年检预约单-取消订单（未支付）
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function cancle_order()
    {
        try {
            $n_id = intval(Request::input('id',0));//年检预约单id
            $n_uid = intval(api_user_id());//用户id

            //开启事务
            shiwu_start();
            $result = OrderCheckpointService::api_cancle_appointment_notpay($n_uid,$n_id);
            shiwu_end($result);

            return  $result;
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }



    /**
     * 用户删除订单
     *
     * @return array|void
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function user_del_order(){
        try {
            $n_uid = intval(api_user_id());
            $n_id = intval(Request::input('id',0));
            return CheckpointOrderService::api_user_del_order($n_uid,$n_id);
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 用户年检预约单列表
     *
     * @return array|string
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function user_order_list()
    {
        try {
            $n_uid = intval(api_user_id());
            $arr_input = Request::input();
            return CheckpointOrderService::api_user_order_list($n_uid,$arr_input);
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }

    }

    /**
     * 用户年检预约单详情
     *
     * @return array|string
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function user_order_detail()
    {
        try {
            $n_uid = intval(api_user_id());
            $arr_input = Request::input();
            return CheckpointOrderService::api_user_order_detail($n_uid,$arr_input);
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 年检预约单-售后列表
     *
     * @return array|string
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function aftersale_list()
    {
        try {
            $n_uid = intval(api_user_id());
            return CheckpointOrderService::api_aftersale_list($n_uid);
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }

    }

    /**
     * 年检预约单-售后详情
     *
     * @return array|string
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function aftersale_detail()
    {
        try {
            $n_uid = intval(api_user_id());
            $n_id = intval(Request::input('id',0));
            return CheckpointOrderService::api_aftersale_detail($n_id,$n_uid);
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 模拟支付
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function simulate_pay(){

        try {
            $user = api_user();
            dd($user['id']);
            $n_id = intval(Request::input('id',0));
            shiwu_start();
            $res = OrderCheckpointService::api_simulate_pay($n_id);
            shiwu_end($res);
            return  $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }

    public function test_pay(){
        try {
            $s_order_type = 'aa';
            $s_pay_type = 'aliapp';
            $arr_pay_data = [
              'out_trade_no'=>'AA000001',
              'total_fee'=>1,
              'body'=>'测试支付宝支付',
            ];
            $result = PayYanSongDaService::api_aliapp_pay($s_order_type,$s_pay_type,$arr_pay_data);

            return  $result;
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }
}